Page({
	data: {
		urlAddress: getApp().globalData.imageUrl,
		showList: null,
		isShow: false,
		code: '',
		isCanSelect: true,
		showFloat: false
	},
	onLoad: function(options) {
		this.setData({
			code: options.code,
		})
	},

	onShow: function() {
		// 页面显示
		//判断是否有用户缓存


		console.log("**pid**");
		console.log(this.data.pid);
		console.log("**pid**");;
		if (this.data.pid && wx.getStorageSync('isChoose')) {
			console.log(wx.getStorageSync('memberId'))
			wx.setStorageSync('isChoose', 0);
			this.setData({
				memId: wx.getStorageSync('memberId')
			})
			var e = {
				target: {
					dataset: {
						pid: this.data.pid,
						code: this.data.oid
					}
				}
			}

			this.chooseAction(e)


		} else {
			this.getUserId();
		}


	},
	chooseAction: function(e) {

		var investigationId = this.data.code;
		var problemId = e.target.dataset.pid;
		var optionId = e.target.dataset.code;
		var memberId = this.data.memId;
		//console.log(investigationId, problemId, optionId, memberId)

		wx.showLoading({
			title: '加载中',
			mask: true

		})
		var _self = this;
		wx.request({
			url: getApp().globalData.otherUrl + '/que/investigation/addOptionMember?investigationId=' + investigationId + '&problemId=' + problemId + '&optionId=' + optionId + '&memberId=' + memberId,
			method: 'POST',
			success: function(res) {
				_self.setData({
					pid: null
				})
				wx.hideLoading();
				if (res.data.code == 1) {
					wx.showToast({
						title: '投票成功',
						icon: 'none',
						duration: 2000
					})
					_self.showView(_self.data.showList, optionId)
				} else {
					wx.showToast({
						title: res.data.message,
						icon: 'none',
						duration: 2000
					})

				}
			}
		})
	},

	getInveDetail: function(code, memId) {
		wx.showLoading({
			title: '加载中',
			mask: true


		})
		var _self = this;
		wx.request({
			url: getApp().globalData.otherUrl + '/que/investigation/queryProblem?investigationId=' + code + '&memberId=' + memId,
			method: 'POST',
			success: function(res) {
				wx.hideLoading();
				if (res.data.code == 1) {
					_self.showView(res.data.data, '')

					if (res.data.data.status != 1) {
						_self.setData({
							showFloat: true
						})
					}

				} else {
					wx.showToast({
						title: '问卷正在发布中，请您稍后在进来看',
						icon: 'none',
						duration: 2000
					})
					_self.setData({
						showFloat: true
					})

				}
			}
		})
	},
	showView: function(data, id) {
		var lin = true;
		for (var i = 0; i < data.listproblem[0].listoption.length; i++) {
			var expectNum = data.listproblem[0].listoption[i].expectNum;
			var memberNum = data.listproblem[0].listoption[i].memberNum;
			if (id == data.listproblem[0].listoption[i].id) {
				memberNum++;
				data.listproblem[0].listoption[i].memberNum = memberNum;
				console.log("***")
				console.log(data.listproblem[0].listoption[i].isSelect)
				data.listproblem[0].listoption[i].isSelect = 1;
				lin = false;
			}
			var bl = memberNum / expectNum > 1 ? 1 : memberNum / expectNum;
			var leng = bl * 452 + 'rpx';
			if (expectNum > memberNum) {} else {
				data.listproblem[0].listoption[i].isFull = true;
			}

			data.listproblem[0].listoption[i].leng = leng;
			if (data.listproblem[0].listoption[i].isSelect) {
				lin = false;
			}
		}
		console.log(data)
		this.setData({
			showList: data,
			isCanSelect: lin
		})

	},
	getUserId: function() {

		var _self = this;
		wx.login({
			success: function(res) {

				if (res.code) {
					var code = res.code;
					console.log(code);
					// return;
					_self.acquireOpenId(code)
				} else {

				}
			},
			fail: function(res) {
				console.log(res)
			}
		});

	},
	//用code获取openId,是否是新用户,并缓存 
	acquireOpenId: function(code) {
		var _self = this;
		wx.showLoading({
			title: '加载中',
			mask: true
		});
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/autoLogin.do',
			data: {
				code: code
			},
			success: function(response) {
				wx.hideLoading();
				const res = response.data
				if (res.code == 200) {
					if (res.obj.first) {
						_self.setData({
							isFirst: true,
							openid: res.obj.openId
						});
						wx.setStorageSync('openId', res.obj.openId);
						wx.setStorageSync('newUser', true);
						_self.getUserInfo();
					} else {
						//老用户   
						_self.setData({
							isFirst: false,
							openid: res.obj.openId
						});
						wx.setStorageSync('memberId', res.obj.memId);
						wx.setStorageSync('openId', res.obj.openId);
						_self.appletLogin();
					}
					return
				} else {
					_self.setData({
						isFirst: false
					});
					_self.showMyToast(_self, res.des)
				}
			}
		})
	},
	//获取用户信息
	getUserInfo: function() {
		this.setData({
			userInfo: null
		})
		getApp().globalData.userInfo.nickName = "";
		getApp().globalData.userInfo.avatarUrl = "";
		getApp().globalData.userInfo.gender = "";
		this.getInveDetail(this.data.code, '')

	},
	//老用户根据openid登录
	appletLogin: function() {
		wx.showLoading({
			title: '加载中',
			mask: true
		});
		var _self = this
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/login.do',
			method: 'get',
			data: {
				openId: wx.getStorageSync('openId'),
			},
			success: function(res) {
				wx.hideLoading();
				if (res.data.code == 200) {
					_self.setData({
						memId: res.data.obj.memId
					})
					_self.getInveDetail(_self.data.code, res.data.obj.memId)

				} else {

				}
			}
		})
	},
	//获取用户手机号
	getPhoneNumber: function(e) {
		console.log(e)
		var _self = this;

		if (!e.detail.iv || !e.detail.encryptedData) {
			console.log('失败')

			_self.setData({

				pid: e.target.dataset.pid,
				oid: e.target.dataset.code

			})



			wx.navigateTo({
				url: '../login/login?code=' + _self.data.code
			})
		} else {

			this.decryption(wx.getStorageSync('openId'), e.detail.iv, e.detail.encryptedData, e.target.dataset.pid, e.target.dataset.code);
		}

	},
	//手机号解密、注册
	decryption: function(id, iv, encryptedData, pid, code) {
		wx.showLoading({
			title: '加载中',
			mask: true
		});
		var _self = this;
		wx.request({
			url: getApp().globalData.baseUrl + '/applet/decryptMobile.do',
			method: 'post',
			// header: getApp().globalData.header,
			data: {
				openId: id,
				iv: iv,
				encryptedData: encryptedData,
				platform: 6,
				shopCode: getApp().globalData.common.shopCode,
				userInfo: _self.data.userInfo,
			},
			success: function(res) {
				wx.hideLoading();
				if (res.data.code == 200) {

					_self.setData({
						isFirst: false,
						memId: res.data.obj.memId
					})

					var e = {
						target: {
							dataset: {
								pid: pid,
								code: code
							}
						}
					}
					console.log(e)
					_self.chooseAction(e)

				} else {
					console.log('失败')

					wx.navigateTo({
						url: '../login/login?code=' + this.data.code
					})
				}
			}
		})

	},
	onShareAppMessage: function(res) {

		console.log(res.target.dataset)
		var shareStr = '';
		if (res.target.dataset.name) {
			shareStr = '小麦厨房即将来到' + res.target.dataset.name + '喽，亲，看你的了'
		} else {
			shareStr = 'hi邻居，小麦厨房搬到您家楼下了'
		}
		if (res.from === 'button') {
			// 来自页面内转发按钮
			wx.showShareMenu({
				withShareTicket: true
			})
		}
		return {
			title: shareStr,
			imageUrl: this.data.urlAddress + '/inv_share.png',
			path: 'pages/investigationDetail/investigationDetail?code=' + this.data.code,
			success: function(res) {
				// 转发成功

			},
			fail: function(res) {
				// 转发失败
			}
		}


	}
})